<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="实例设备">
              <j-search-select-tag v-model="queryParam.instanceId" dict="iot_device_instance,name,id" placeholder="请选择实例设备"/>
            </a-form-item>
          </a-col>
          <a-col v-show="toggleSearchStatus" :lg="11" :md="12" :sm="24" :xl="10">
            <a-form-item label="时间段">
              <j-date v-model="queryParam.createTime_begin" :show-time="true" class="query-group-cust" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date v-model="queryParam.createTime_end" :show-time="true" class="query-group-cust" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间"></j-date>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <span class="table-page-search-submitButtons" style="float: left;overflow: hidden;">
              <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
              <a-button icon="reload" style="margin-left: 8px" type="primary" @click="searchReset">重置</a-button>
              <a style="margin-left: 8px" @click="handleToggleSearch">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('设备上传数据')">导出</a-button>
      <a-upload :action="importExcelUrl" :headers="tokenHeader" :multiple="false" :showUploadList="false" name="file" @change="handleImportExcel">
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange">

        <template slot="dataSlot" slot-scope="dataMaps, record">
          <span v-for="data in dataMaps">
            <span style="font-weight: bold">{{ data.name }}</span>：{{ data.formatValue+(data.metadata.unit||'') }}&nbsp;
            {{data.paramAlarm}}
<!--            <span v-if="data.paramAlarm">-->
<!--              <a-tag :color="alarmLevelOptions[data.paramAlarm.alarmLevel].color">-->
<!--                <span v-if="alarmLevelOptions[data.paramAlarm.alarmLevel].value === 0">{{ alarmLevelOptions[data.paramAlarm.alarmLevel].text }}</span>-->
<!--                <span v-else>{{ alarmLevelOptions[data.paramAlarm.alarmLevel].text }}告警</span>-->
<!--              </a-tag>-->
<!--            </span>-->
          </span>
        </template>

        <template slot="reportModeSlot" slot-scope="type">
          <a-tag v-if="type===1" color="green">上报</a-tag>
          <a-tag v-else color="gold">手动</a-tag>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">查看</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

      </a-table>
    </div>

    <data-report-modal ref="modalForm" @ok="modalFormOk"></data-report-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import DataReportModal from './modules/DataReportModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import JDate from '@/components/jeecg/JDate.vue'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import {getAction} from '@api/manage'

export default {
  name: 'DataReportList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    JDate,
    JSearchSelectTag,
    DataReportModal
  },
  data: function () {
    return {
      // description: '设备上传数据管理页面',
      alarmLevelOptions: [],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '设备实例',
          align: 'center',
          dataIndex: 'instanceName'
        },
        {
          title: '数据',
          align: 'left',
          dataIndex: 'reportParams',
          key: 'reportParams',
          scopedSlots: {customRender: 'dataSlot'}
        },
        {
          title: '类型',
          align: 'center',
          dataIndex: 'reportMode',
          scopedSlots: {customRender: 'reportModeSlot'}
        },
        {
          title: '时间',
          align: 'center',
          dataIndex: 'reportTime'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/message/data/list',
        delete: '/message/data/delete',
        deleteBatch: '/message/data/deleteBatch',
        exportXlsUrl: '/message/data/exportXls',
        importExcelUrl: 'message/data/importExcel'
      },
      dictOptions: {}
    }
  },
  watch: {},
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  mounted() {
    this.getAlarmLevel()
  },
  methods: {
    getAlarmLevel() {
      let scope = this
      getAction('alarm/config/getAlarmLevel').then(data => {
        if (data.success) {
          scope.alarmLevelOptions = data.result
        }
      })
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
